package com.qianyu.project.mapper.provider.sy;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

import java.util.Map;

public class ProgressPaymentProvider {

    public String getList(IPage page, Map<String, String> params){
        return new SQL(){
            {
                SELECT("a.*, p.project_name, i.contract_name, t.company_name");
                FROM("progress_payment a, project_info p, income_contract i, tender_company t");
                WHERE("a.pid = p.id and a.ctr_id = i.id and a.company_id = t.id ");
                if(StringUtils.hasLength(params.get("id"))){
                    WHERE(" a.id = #{params.id}");
                }
                if(StringUtils.hasLength(params.get("paymentName"))){
                    WHERE(" a.payment_name like '%${params.paymentName}%'");
                }
                if(StringUtils.hasLength(params.get("pid"))){
                    WHERE(" a.pid = #{params.pid}");
                }
                if (StringUtils.hasLength(params.get("approveStatus"))){
                    WHERE(" a.approve_status = #{params.approveStatus}");
                }
                if (StringUtils.hasLength(params.get("ctrId"))){
                    WHERE(" a.ctr_id = #{params.ctrId}");
                }
                if (StringUtils.hasLength(params.get("handlerId"))){
                    WHERE(" a.handler_id like '%${params.handlerId}%'");
                }
                if(StringUtils.hasLength(params.get("applyDate1")) && StringUtils.hasLength(params.get("applyDate2"))){
                    WHERE(" a.apply_date >= '${params.applyDate1}' and a.apply_date <= '${params.applyDate2}'");
                }
                if(StringUtils.hasLength(params.get("companyId"))){
                    WHERE(" a.company_id = #{params.companyId}");
                }
            }
        }.toString();
    }

    public String getOne(String id) {
        return new SQL() {
            {
                SELECT("a.*, p.project_name, i.contract_name, t.company_name");
                FROM("progress_payment a, project_info p, income_contract i, tender_company t");
                WHERE("a.pid = p.id and a.ctr_id = i.id and a.company_id = t.id and a.id = #{id}");
            }
        }.toString();
    }

    /**
     * 进度款申报查询
     * @param page
     * @param params
     * @return
     */
    public String getProgressQuery(IPage page, Map<String, String> params){
        return new SQL(){
            {
                SELECT("a.*, p.project_name, i.contract_name, t.company_name, c.change_amount");
                FROM("progress_payment a, project_info p, income_contract i, tender_company t, contract_change c");
                WHERE("a.pid = p.id and a.ctr_id = i.id and a.company_id = t.id and a.ctr_id = c.ctr_id");
                if(StringUtils.hasLength(params.get("id"))){
                    WHERE(" a.id = #{params.id}");
                }
                if(StringUtils.hasLength(params.get("paymentName"))){
                    WHERE(" a.payment_name like '%${params.paymentName}%'");
                }
                if(StringUtils.hasLength(params.get("pid"))){
                    WHERE(" a.pid = #{params.pid}");
                }
                if (StringUtils.hasLength(params.get("approveStatus"))){
                    WHERE(" a.approve_status = #{params.approveStatus}");
                }
                if (StringUtils.hasLength(params.get("ctrId"))){
                    WHERE(" a.ctr_id = #{params.ctrId}");
                }
                if (StringUtils.hasLength(params.get("handlerId"))){
                    WHERE(" a.handler_id like '%${params.handlerId}%'");
                }
                if(StringUtils.hasLength(params.get("applyDate1")) && StringUtils.hasLength(params.get("applyDate2"))){
                    WHERE(" a.apply_date >= '${params.applyDate1}' and a.apply_date <= '${params.applyDate2}'");
                }
                if(StringUtils.hasLength(params.get("companyId"))){
                    WHERE(" a.company_id = #{params.companyId}");
                }
            }
        }.toString();
    }


}
